package user_private_key

import (
	"encoding/json"
	"github.com/xinliangnote/go-gin-api/internal/code"
	"github.com/xinliangnote/go-gin-api/internal/pkg/core"
	"github.com/xinliangnote/go-gin-api/internal/pkg/validation"
	userModel "github.com/xinliangnote/go-gin-api/internal/repository/mongodb/user"
	"github.com/xinliangnote/go-gin-api/internal/services/user_private_key"
	"github.com/xinliangnote/go-gin-api/internal/tools"
	"net/http"
)

type UpdateParams struct {
	Id        string `json:"id"`
	ChainName string `json:"chainName"`
}

func (h *handler) Update() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(createRequest)
		res := new(createResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		if req.Code == "" {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.EmailCodeError,
				code.Text(code.EmailCodeError)),
			)
			return
		}

		userInfo := ctx.GetInfo("userInfo").(*userModel.User)
		redisCode, _ := h.cache.Get(userInfo.Email + "_email_code")
		if req.Code != redisCode {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.EmailCodeError,
				code.Text(code.EmailCodeError)),
			)
			return
		}

		//userInfo := ctx.GetInfo("userInfo").(*user.User)
		createData := new(user_private_key.UserPrivateKeyData)
		createData.Id = req.Id
		createData.CollectAddress = req.CollectAddress
		//createData.FeeAddress = req.FeeAddress
		//createData.WithdrawAddress = req.WithdrawAddress
		err := h.keyService.Update(ctx, createData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.UserPrivateKeyUpdateError,
				code.Text(code.UserPrivateKeyUpdateError)).WithError(err),
			)
			return
		}

		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}

func (h *handler) UpdateAddress() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(UpdateParams)
		res := new(createResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		var flag string
		if req.ChainName == "TRX" {
			flag = "TRX"
		} else {
			flag = "EVM"
		}

		url := "http://47.242.17.13:8888/api/generateAddress?flag=" + flag
		body, err := tools.SendGet(url)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.SendGetError,
				code.Text(code.SendGetError)),
			)
			return
		}

		var data AddressData
		err = json.Unmarshal(body, &data)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.JsonUnmarshalError,
				code.Text(code.JsonUnmarshalError)),
			)
			return
		}
		if data.Code != 200 {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.JsonUnmarshalError,
				data.Msg),
			)
			return
		}
		createData := new(user_private_key.UserPrivateKeyData)
		createData.Id = req.Id
		createData.FeeAddress = data.Data.Address
		err = h.keyService.Update(ctx, createData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.UserPrivateKeyCreateError,
				code.Text(code.UserPrivateKeyCreateError)).WithError(err),
			)
			return
		}

		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
